<?php

namespace app\admin\controller\union;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\union\Guild as GuildModel;
use app\admin\model\union\GuildAccountDaily;
use app\common\controller\Backend;

class Guild extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    protected $guild_id = 0;
    protected $guild_manage_id = 0;
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $this->guild_id = Admin::get(intval($auth->id))['guild_id'];

        $this->model = new GuildModel();

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $child_id = [];
            $child_data = $this->model->field('id')->where('parent_guild_id',$this->guild_id)->select()->toArray();
            if (!empty($child_data)){
                $child_id = array_column($child_data,'id');
            }

            $filter = json_decode($this->request->get('filter'),true);
            $op = json_decode($this->request->get('op'),true);
            $where1 = [];
            if (isset($op['created_at'])){
                $where1 = explode(' - ',$filter['created_at']);
                unset($op['created_at']);
                unset($filter['created_at']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where($where)->where('id','in',$child_id)->order($sort, $order)->paginate($limit);
            foreach ($list->items() as &$v){
                if (isset($child_id)){
                    //field('dt,sum(one_guild_income) as child_sum')->
                    $tmp = GuildAccountDaily::where('guild_manage_id','=',$v->id);
                  //  print_r($where1);
                    if ($where1){
                        if (!empty($where1[1])){
                            $tmp = $tmp->where('dt','between time',[date('Y-m-d',strtotime($where1[0])),date('Y-m-d',strtotime($where1[1]))]);
                        }else{
                            $tmp = $tmp->where('dt',date('Y-m-d',strtotime($where1[0])));
                        }
                    }
                    $v->child_sum = $tmp->value("sum(one_guild_income)");
                }else{
                    $v->child_sum = 0;
                }
                $v->child_sum = round($v->child_sum,2);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }

}